/**
 * 
 */
package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.rs.model.ZhGrade;
import com.rs.service.ZhGradeService;
import com.rs.util.SpringContextUtils;










import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author zjw
 *
 */
@SuppressWarnings("serial")
public class GradeUpdate extends JFrame {

	private JPanel contentPane;
	private JTextField txt_stuname;
	private JTextField txt_teaname;
	private JTextField txt_graDate;
	private JTextField txt_clname;
	private JTextField txt_subname;
	private JTextField txt_score;
	int gid;
	ZhGradeService gradeservice = SpringContextUtils.getBean(ZhGradeService.class);
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GradeUpdate frame = new GradeUpdate(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GradeUpdate(final JDesktopPane jdesktoppane) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				GradeList gradelist = new GradeList(jdesktoppane);
				gradelist.setVisible(true);
				jdesktoppane.add(gradelist);
			}
		});
	
		setResizable(false);
		setTitle("学生成绩信息");
		setBounds(100, 100, 578, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel lb_stuname = new JLabel("学生名称：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_stuname, 46, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_stuname, 55, SpringLayout.WEST, contentPane);
		contentPane.add(lb_stuname);
		
		txt_stuname = new JTextField();
		txt_stuname.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_stuname, 25, SpringLayout.EAST, lb_stuname);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, txt_stuname, -304, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_stuname, -304, SpringLayout.EAST, contentPane);
		contentPane.add(txt_stuname);
		txt_stuname.setColumns(10);
		
		JLabel lb_graDate = new JLabel("测试时间：");
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_graDate, -204, SpringLayout.EAST, contentPane);
		contentPane.add(lb_graDate);
		
		txt_graDate = new JTextField();
		txt_graDate.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_graDate, -3, SpringLayout.NORTH, lb_graDate);

		contentPane.add(txt_graDate);
		txt_graDate.setColumns(10);
		
		JLabel lb_teaname = new JLabel("老师名称：");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lb_teaname, -242, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_teaname, 0, SpringLayout.WEST, lb_stuname);
		contentPane.add(lb_teaname);
		
		txt_teaname = new JTextField();
		txt_teaname.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_teaname, 49, SpringLayout.SOUTH, txt_stuname);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_teaname, 25, SpringLayout.EAST, lb_teaname);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_teaname, 0, SpringLayout.EAST, txt_stuname);
		contentPane.add(txt_teaname);
		txt_teaname.setColumns(10);
		
		JLabel lb_clname = new JLabel("班级名称：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_clname, 48, SpringLayout.SOUTH, lb_teaname);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_clname, 0, SpringLayout.EAST, lb_stuname);
		contentPane.add(lb_clname);
		
		txt_clname = new JTextField();
		txt_clname.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_clname, -3, SpringLayout.NORTH, lb_clname);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_clname, 0, SpringLayout.WEST, txt_stuname);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_clname, -304, SpringLayout.EAST, contentPane);
		contentPane.add(txt_clname);
		txt_clname.setColumns(10);
		
		JLabel lb_subname = new JLabel("测试科目：");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_subname, 0, SpringLayout.NORTH, lb_clname);
		sl_contentPane.putConstraint(SpringLayout.EAST, lb_subname, 0, SpringLayout.EAST, lb_graDate);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, lb_teaname, -242, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_teaname, 0, SpringLayout.WEST, lb_stuname);
		contentPane.add(lb_subname);
		
		txt_subname = new JTextField();
		txt_subname.setEditable(false);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_subname, -3, SpringLayout.NORTH, lb_clname);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_subname, 26, SpringLayout.EAST, lb_subname);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_subname, 0, SpringLayout.EAST, txt_graDate);
		contentPane.add(txt_subname);
		txt_subname.setColumns(10);
		
		JLabel lb_score = new JLabel("测试成绩：");
		sl_contentPane.putConstraint(SpringLayout.WEST, lb_score, 298, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_graDate, 55, SpringLayout.SOUTH, lb_score);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_graDate, 55, SpringLayout.SOUTH, lb_score);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_stuname, -40, SpringLayout.WEST, lb_score);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lb_score, 0, SpringLayout.NORTH, lb_stuname);
		contentPane.add(lb_score);
		
		txt_score = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_graDate, 0, SpringLayout.WEST, txt_score);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_graDate, 0, SpringLayout.EAST, txt_score);
		sl_contentPane.putConstraint(SpringLayout.WEST, txt_score, 384, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, txt_score, -67, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, txt_score, -3, SpringLayout.NORTH, lb_stuname);
		contentPane.add(txt_score);
		txt_score.setColumns(10);
		
		JButton btn_update = new JButton("修改");
		sl_contentPane.putConstraint(SpringLayout.NORTH, btn_update, 58, SpringLayout.SOUTH, txt_clname);
		sl_contentPane.putConstraint(SpringLayout.WEST, btn_update, 209, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btn_update, -231, SpringLayout.EAST, contentPane);
		btn_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhGrade grade = new ZhGrade();
				grade.setGra_id(gid);
//				grade.setCl_name(txt_clname.getText());
				grade.setGra_score(Integer.parseInt(txt_score.getText()));
				gradeservice.updateGrade(grade);
				JOptionPane.showMessageDialog(new JButton(), "修改成功！");

			}
		});
		contentPane.add(btn_update);
		
	}
	
	public void select(Object id){
	
		ZhGrade grade = new ZhGrade();
		grade.setGra_id(Integer.parseInt(id+""));
		grade = gradeservice.selectGrade(grade).getList().get(0);
		txt_clname.setText(grade.getCl_name());
		txt_graDate.setText(grade.getGra_dates());
		txt_score.setText(grade.getGra_score()+"");
		txt_stuname.setText(grade.getStu_name());
		txt_subname.setText(grade.getGra_subject());
		txt_teaname.setText(grade.getTea_name());
		gid = Integer.parseInt(id+"");
	}
}
